#ifndef PROJECTLOADER_H
#define PROJECTLOADER_H

#include <wx/hashmap.h>
#include "ibaseloader.h"

class M_Code_Project;
class ProjectBuildTarget;
class ProjectFile;

WX_DECLARE_STRING_HASH_MAP( wxString, CompilerSubstitutes );

class ProjectLoader : public IBaseLoader {
  public:
    ProjectLoader( M_Code_Project* project );
    virtual ~ProjectLoader();
    bool Open( const wxString& filename );
    bool Save( const wxString& filename );
    bool Open( const wxString& filename, TiXmlElement** ppExtensions );
    bool Save( const wxString& filename, TiXmlElement* pExtensions );
    bool ExportTargetAsProject( const wxString& filename, const wxString& onlyTarget, TiXmlElement* pExtensions );
    bool FileUpgraded() { return m_Upgraded; }
    bool FileModified() { return m_OpenDirty; }
  protected:
    void DoProjectOptions( TiXmlElement* parentNode );
    void DoCompilerOptions( TiXmlElement* parentNode, ProjectBuildTarget* target = nullptr );
    void DoResourceCompilerOptions( TiXmlElement* parentNode, ProjectBuildTarget* target = nullptr );
    void DoLinkerOptions( TiXmlElement* parentNode, ProjectBuildTarget* target = nullptr );
    void DoIncludesOptions( TiXmlElement* parentNode, ProjectBuildTarget* target = nullptr );
    void DoLibsOptions( TiXmlElement* parentNode, ProjectBuildTarget* target = nullptr );
    void DoExtraCommands( TiXmlElement* parentNode, ProjectBuildTarget* target = nullptr );
    void DoMakeCommands( TiXmlElement* parentNode, CompileTargetBase* target );
    void DoVirtualTargets( TiXmlElement* parentNode );
    void DoBuild( TiXmlElement* parentNode );
    void DoBuildTarget( TiXmlElement* parentNode );
    void DoBuildTargetOptions( TiXmlElement* parentNode, ProjectBuildTarget* target );
    void DoEnvironment( TiXmlElement* parentNode, CompileOptionsBase* base );
    void DoUnits( const TiXmlElement* parentNode );
    void DoUnitOptions( const TiXmlElement* parentNode, ProjectFile* file );
  private:
    void ConvertLibraries( CompileTargetBase* object );
    TiXmlElement* AddElement( TiXmlElement* parent, const char* name, const char* attr = nullptr, const wxString& attribute = wxEmptyString );
    TiXmlElement* AddElement( TiXmlElement* parent, const char* name, const char* attr, int attribute );
    void AddArrayOfElements( TiXmlElement* parent, const char* name, const char* attr, const wxArrayString& array, bool isPath = false );
    void SaveEnvironment( TiXmlElement* parent, CompileOptionsBase* base );
    wxString GetValidCompilerID( const wxString& proposal, const wxString& scope );
    ProjectLoader() {}
    M_Code_Project* m_pProject;
    bool m_Upgraded;
    bool m_OpenDirty;
    int m_1_4_to_1_5_deftarget;
    CompilerSubstitutes m_CompilerSubstitutes;
};

#endif
